import request from './request'

const SHARE_PREFIX = '/share'

/**
 * 媒体分享相关接口
 */

// 创建分享链接
export function createShareLink(options: {
  mediaIds: number[]
  shareType: 'public' | 'private' | 'family'
  expireTime?: string
  password?: string
  allowDownload?: boolean
  allowComment?: boolean
}) {
  return request({
    url: `${SHARE_PREFIX}/create`,
    method: 'post',
    data: options
  })
}

// 获取分享链接详情
export function getShareLink(shareId: string, password?: string) {
  return request({
    url: `${SHARE_PREFIX}/${shareId}`,
    method: 'get',
    params: { password }
  })
}

// 更新分享设置
export function updateShareSettings(shareId: string, settings: {
  expireTime?: string
  password?: string
  allowDownload?: boolean
  allowComment?: boolean
  isEnabled?: boolean
}) {
  return request({
    url: `${SHARE_PREFIX}/${shareId}/settings`,
    method: 'put',
    data: settings
  })
}

// 删除分享链接
export function deleteShareLink(shareId: string) {
  return request({
    url: `${SHARE_PREFIX}/${shareId}`,
    method: 'delete'
  })
}

// 获取我的分享列表
export function getMyShares(params?: {
  page?: number
  size?: number
  shareType?: string
  status?: 'active' | 'expired' | 'disabled'
}) {
  return request({
    url: `${SHARE_PREFIX}/my-shares`,
    method: 'get',
    params
  })
}

// 获取分享访问统计
export function getShareStatistics(shareId: string) {
  return request({
    url: `${SHARE_PREFIX}/${shareId}/statistics`,
    method: 'get'
  })
}

/**
 * 权限管理相关接口
 */

// 获取用户权限
export function getUserPermissions(userId: number) {
  return request({
    url: `${SHARE_PREFIX}/permissions/user/${userId}`,
    method: 'get'
  })
}

// 设置媒体文件权限
export function setMediaPermissions(mediaId: number, permissions: {
  isPublic: boolean
  allowedUsers: number[]
  viewPermission: 'all' | 'family' | 'custom'
  downloadPermission: 'all' | 'family' | 'owner' | 'none'
  commentPermission: 'all' | 'family' | 'none'
}) {
  return request({
    url: `${SHARE_PREFIX}/permissions/media/${mediaId}`,
    method: 'post',
    data: permissions
  })
}

// 批量设置权限
export function batchSetPermissions(mediaIds: number[], permissions: {
  isPublic: boolean
  allowedUsers: number[]
  viewPermission: 'all' | 'family' | 'custom'
  downloadPermission: 'all' | 'family' | 'owner' | 'none'
  commentPermission: 'all' | 'family' | 'none'
}) {
  return request({
    url: `${SHARE_PREFIX}/permissions/batch`,
    method: 'post',
    data: { mediaIds, permissions }
  })
}

// 获取媒体文件权限设置
export function getMediaPermissions(mediaId: number) {
  return request({
    url: `${SHARE_PREFIX}/permissions/media/${mediaId}`,
    method: 'get'
  })
}

/**
 * 社交分享相关接口
 */

// 分享到社交平台
export function shareToSocial(options: {
  mediaIds: number[]
  platform: 'wechat' | 'weibo' | 'qq' | 'facebook' | 'twitter' | 'instagram'
  message?: string
  tags?: string[]
}) {
  return request({
    url: `${SHARE_PREFIX}/social`,
    method: 'post',
    data: options
  })
}

// 生成社交分享内容
export function generateSocialContent(mediaIds: number[], platform: string) {
  return request({
    url: `${SHARE_PREFIX}/social/generate`,
    method: 'post',
    data: { mediaIds, platform }
  })
}

/**
 * 评论和互动相关接口
 */

// 获取分享评论
export function getShareComments(shareId: string, params?: {
  page?: number
  size?: number
  sortBy?: 'time' | 'likes'
}) {
  return request({
    url: `${SHARE_PREFIX}/${shareId}/comments`,
    method: 'get',
    params
  })
}

// 添加分享评论
export function addShareComment(shareId: string, content: string, parentId?: number) {
  return request({
    url: `${SHARE_PREFIX}/${shareId}/comments`,
    method: 'post',
    data: { content, parentId }
  })
}

// 点赞分享
export function likeShare(shareId: string) {
  return request({
    url: `${SHARE_PREFIX}/${shareId}/like`,
    method: 'post'
  })
}

// 取消点赞
export function unlikeShare(shareId: string) {
  return request({
    url: `${SHARE_PREFIX}/${shareId}/unlike`,
    method: 'post'
  })
}

// 收藏分享
export function favoriteShare(shareId: string) {
  return request({
    url: `${SHARE_PREFIX}/${shareId}/favorite`,
    method: 'post'
  })
}

// 取消收藏
export function unfavoriteShare(shareId: string) {
  return request({
    url: `${SHARE_PREFIX}/${shareId}/unfavorite`,
    method: 'post'
  })
}

/**
 * 访问控制相关接口
 */

// 检查访问权限
export function checkAccess(mediaId: number) {
  return request({
    url: `${SHARE_PREFIX}/access/check/${mediaId}`,
    method: 'get'
  })
}

// 请求访问权限
export function requestAccess(mediaId: number, message?: string) {
  return request({
    url: `${SHARE_PREFIX}/access/request/${mediaId}`,
    method: 'post',
    data: { message }
  })
}

// 批准访问请求
export function approveAccess(requestId: number) {
  return request({
    url: `${SHARE_PREFIX}/access/approve/${requestId}`,
    method: 'post'
  })
}

// 拒绝访问请求
export function rejectAccess(requestId: number, reason?: string) {
  return request({
    url: `${SHARE_PREFIX}/access/reject/${requestId}`,
    method: 'post',
    data: { reason }
  })
}

// 获取访问请求列表
export function getAccessRequests(params?: {
  page?: number
  size?: number
  status?: 'pending' | 'approved' | 'rejected'
}) {
  return request({
    url: `${SHARE_PREFIX}/access/requests`,
    method: 'get',
    params
  })
}

/**
 * 家庭成员管理相关接口
 */

// 获取家庭成员列表
export function getFamilyMembers() {
  return request({
    url: `${SHARE_PREFIX}/family/members`,
    method: 'get'
  })
}

// 邀请家庭成员
export function inviteFamilyMember(options: {
  email: string
  name: string
  role: 'admin' | 'member' | 'viewer'
  permissions: string[]
}) {
  return request({
    url: `${SHARE_PREFIX}/family/invite`,
    method: 'post',
    data: options
  })
}

// 更新成员权限
export function updateMemberPermissions(memberId: number, permissions: {
  role: 'admin' | 'member' | 'viewer'
  canUpload: boolean
  canDownload: boolean
  canShare: boolean
  canDelete: boolean
  canManageAlbums: boolean
}) {
  return request({
    url: `${SHARE_PREFIX}/family/members/${memberId}/permissions`,
    method: 'put',
    data: permissions
  })
}

// 移除家庭成员
export function removeFamilyMember(memberId: number) {
  return request({
    url: `${SHARE_PREFIX}/family/members/${memberId}`,
    method: 'delete'
  })
}

/**
 * TypeScript 类型定义
 */

export interface ShareLink {
  id: string
  title: string
  mediaIds: number[]
  shareType: 'public' | 'private' | 'family'
  shareUrl: string
  shortUrl: string
  expireTime?: Date
  password?: string
  allowDownload: boolean
  allowComment: boolean
  isEnabled: boolean
  createTime: Date
  creator: {
    id: number
    name: string
    avatar?: string
  }
  statistics: {
    viewCount: number
    downloadCount: number
    commentCount: number
    likeCount: number
  }
}

export interface MediaPermission {
  mediaId: number
  isPublic: boolean
  allowedUsers: number[]
  viewPermission: 'all' | 'family' | 'custom'
  downloadPermission: 'all' | 'family' | 'owner' | 'none'
  commentPermission: 'all' | 'family' | 'none'
  updateTime: Date
}

export interface ShareComment {
  id: number
  shareId: string
  content: string
  authorId: number
  authorName: string
  authorAvatar?: string
  parentId?: number
  replies: ShareComment[]
  likeCount: number
  isLiked: boolean
  createTime: Date
}

export interface FamilyMember {
  id: number
  name: string
  email: string
  avatar?: string
  role: 'admin' | 'member' | 'viewer'
  joinTime: Date
  lastActiveTime: Date
  permissions: {
    canUpload: boolean
    canDownload: boolean
    canShare: boolean
    canDelete: boolean
    canManageAlbums: boolean
  }
}

export interface AccessRequest {
  id: number
  mediaId: number
  mediaTitle: string
  requesterId: number
  requesterName: string
  requesterEmail: string
  message?: string
  status: 'pending' | 'approved' | 'rejected'
  reason?: string
  createTime: Date
  handleTime?: Date
}

export interface SocialShareResult {
  platform: string
  success: boolean
  shareUrl?: string
  shareId?: string
  error?: string
}